/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import bean.BarangGudang;
import bean.Pemesanan;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author 
 */
@WebServlet(urlPatterns = {"/PesanBarang"})
public class PesanBarang extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        BarangGudang barang = new BarangGudang();
        String kode = request.getParameter("kodepemesanan");
        String jumlah = request.getParameter("jumlahbarang");
        String id = request.getParameter("idbarang");
        String pemesan = request.getParameter("username");



//        double jum = Double.parseDouble(jumlah_barang);
        int jum = Integer.parseInt(jumlah);
        barang.setJumlahBarang(jum);
        barang.setIdBarang(id);

        Pemesanan pesan = new Pemesanan();
        pesan.setKodePemesanan(kode);
        pesan.setJumlahBarang(jum);
        pesan.setIdBarang(id);
        pesan.setUsername(pemesan);

//        System.out.println(pesan.getKodePemesanan());
        if ("".equals(jumlah) || "".equals(kode)) {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("form_pesan_barang.jsp");
            requestDispatcher.forward(request, response);
        } else {

            Cookie[] ck = request.getCookies();
            pesan.setUsername(ck[1].getValue());
            //            pemesanan.insertPemesanan();
            try {
                Connection connection = null;
                OracleDataSource ds;

                ds = new OracleDataSource();
                ds.setURL("jdbc:oracle:thin:@172.23.9.185:1521:orcl");
                ds.setPassword("mhs115314034");
                ds.setUser("mhs115314034");
//                ds.setURL("jdbc:oracle:thin:@localhost:1521:xe");
//            ds.setPassword("hr");
//            ds.setUser("hr");
                connection = (Connection) ds.getConnection();
                Statement statement = connection.createStatement();
                statement.executeQuery("insert into pemesananbarang values ('" + kode + "',"
                        + jum + ",'" + id + "','" + pemesan + "')");
//                statement.executeQuery("insert into pemesananbarang values ('"
//                        + pesan.getKodePemesanan() + "',"
//                        + pesan.getJumlahBarang() + ",'"
//                        + pesan.getNamaBarang() + "','"
//                        + pesan.getUsername() + "')");
                barang.kurangBarang(barang);
                System.out.println("data berhasil diinputkan");
            } catch (SQLException ex) {
                Logger.getLogger(PesanBarang.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       
        out.println("<html>");
        out.println("<head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + "<title>Login</title></head>");
        out.println("<body bgcolor='blue'>" + "<table border='0' align='center'>" + "<th colspan='2'rowspan='1' width='1%' bgcolor='#red' ><h1> <font color='white'" + "<h1 align='center' style='color:#00CCFF'> Rekayasa Perangkat Lunak Berorientasi Proses </h1>" + "<h2 align='center' style='color:#00CCFF'> Sistem Informasi Gudang Alfamart </h2>" + "<h3 align='center' style='color:#00CCFF'> Kelompok ESCAMPUR </h3>" + "</font>" + " </table>");
        out.println("<form action=\"form_utama_toko.jsp\">" + "<table align=\"center\" border=\"0\">");
        out.println("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"kembali\" value=\"kembali ke form utama\" width=\"250\"/></td>" + "</tr></table>"
               +"</form>");
        out.println("</body>");
        out.println("</html>");



    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
